package com.budwk.app.util.poiUtils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.budwk.app.sqfw.dto.FireHiddenCheckWordDTO;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.deepoove.poi.data.PictureRenderData;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Map;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.*;

/**
 * @author ZY.Wang
 * @since 2023/7/27
 */
public class GasInspectionOfficeUtils {

    /**
     * 生成煤气排查的pdf
     *
     * @param wordDTO 生成结果
     * @return 结果
     */
    public static ByteArrayOutputStream createGasInspectionCheckForm(FireHiddenCheckWordDTO wordDTO) {

        // 这个方法的逻辑是这样的，如果填写了煤气排查的信息将会生成煤气排查表pdf，如果没有的话那么就只会将安全排查的图片单独生成一个pdf
        // 如果没有图片就直接返回null就行

        long startTime = System.currentTimeMillis();

        Map<String, Object> paramMap = Maps.newHashMap();
        if (StringUtils.isNotBlank(wordDTO.getA46())) {
            // 填充生成pdf所需要的参数
            popularGasInspectionParamMap(paramMap, wordDTO);
        }

        ByteArrayOutputStream gasInspectOS = new ByteArrayOutputStream();
        if (paramMap.size() > 0) {
            gasInspectOS = generateWordDocument(paramMap, "gasInspection.docx", "煤气检查", startTime);
        }

        return gasInspectOS;
    }

    /**
     * 填充煤气检查填充生成pdf的参数
     *
     * @param paramMap    参数列表
     * @param formSummary 数据dto
     */
    private static void popularGasInspectionParamMap(Map<String, Object> paramMap, FireHiddenCheckWordDTO formSummary) {
        if (StringUtils.isBlank(formSummary.getA46())) return;

        paramMap.put("a1", formSummary.getA1());

        Date fillDate = formSummary.getFillDate();
        if(fillDate == null) fillDate = formSummary.getCreateDate();
        paramMap.put("fillDateStr", DateUtil.format(fillDate, "yyyy-MM-dd"));

        String a19 = formSummary.getA19();
        PictureRenderData a19Pic = addWordPicture(a19, 1.2F);
        paramMap.put("a19", a19Pic);

        String a18 = formSummary.getA18();
        if (StringUtils.isNotBlank(a18)) {
            // 模板的格式应该为 {{a18NoSig}}{{?a18Pics}}{{@a18Pic}}{{/a18Pics}}
            addWordUserSignature(paramMap, "a18NoSig", "a18Pics", "a18Pic", formSummary.getA18NoSignatureUsers(), formSummary.getA18UserSignatureUrls(), 1.2F);
        }

        DateTime createDateTime = new DateTime(fillDate);
        int createDateYear = createDateTime.year();
        int createDateMonth = createDateTime.month();
        int createDateDay = createDateTime.dayOfMonth();
        paramMap.put("fillDateYear", createDateYear);
        paramMap.put("fillDateMonth", createDateMonth + 1);
        paramMap.put("fillDateDay", createDateDay);

        paramMap.put("a8", formSummary.getA8());

        Field[] fields = Sqfw_formSummary.class.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            if (!fieldName.startsWith("a")) continue;

            int fieldIndex = 0;
            try {
                fieldIndex = Integer.parseInt(fieldName.replace("a", ""));
            } catch (Exception ignored) {
            }

            if (fieldIndex == 0) continue;

            if (fieldIndex >= 46 && fieldIndex <= 65) {
                try {
                    Object fieldValueObj = field.get(formSummary);

                    String fieldValue = " ";
                    if (fieldValueObj != null) fieldValue = (String) fieldValueObj;

                    paramMap.put(fieldName, fieldValue);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
